package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.OrderConfirmFooterBean
import com.onlyphoto.onlymall.events.ShowCouponsEvent
import com.onlyphoto.onlymall.utils.NumUtil

import me.drakeet.multitype.ItemViewBinder
import org.greenrobot.eventbus.EventBus

/**
 * Created by gaochujia on 2019-08-27.
 */
class OrderConfirmFooterBinder : ItemViewBinder<OrderConfirmFooterBean, OrderConfirmFooterBinder.ViewHolder>() {


    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.order_confirm_footer, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: OrderConfirmFooterBean) {
        viewHolder.update(bean)
        viewHolder.llCoupon?.setOnClickListener{
            EventBus.getDefault().post(ShowCouponsEvent())
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var couponName: TextView? = null
        var llCoupon: LinearLayout? = null
        var totalPrice: TextView? = null
        var freight: TextView? = null
        init {
            couponName = itemView.findViewById(R.id.coupon_name)
            llCoupon = itemView.findViewById(R.id.ll_coupon)
            totalPrice = itemView.findViewById(R.id.total_price)
            freight = itemView.findViewById(R.id.freight)
        }

        @SuppressLint("SetTextI18n")
        fun update(bean: OrderConfirmFooterBean) {
            totalPrice?.text = "¥ " + NumUtil.getTwoDecimal(bean.totalPrice.toString())
            freight?.text = "¥ " + bean.freight.toString()
            couponName?.text = bean.couponName
        }
    }
}
